/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuardarExcel.java
 *
 * Created on 19/05/2011, 13:02:21
 */

package panaderialabaguette.vista;

import java.awt.Toolkit;
import java.io.File;
import javax.swing.JTable;
import panaderialabaguette.ExcelTableExporter;

/**
 *
 * @author oscar
 */
public class GuardarExcel extends javax.swing.JFrame {

    private JTable tabla;
    /** Creates new form GuardarExcel */
    public GuardarExcel(JTable tabla) {
        this.tabla = tabla;
        initComponents();
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("imagenes/guardar2.png")));
        this.setLocationRelativeTo(null);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();

        setTitle("Guardar tabla como archivo excel");

        jFileChooser1.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
        jFileChooser1.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
        jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFileChooser1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 589, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFileChooser1ActionPerformed
        // TODO add your handling code here:

        if (evt.getActionCommand().equals("ApproveSelection"))
        {
            String ruta = jFileChooser1.getCurrentDirectory().getPath();
            String nombreArch = jFileChooser1.getSelectedFile().getName();
            String nombreArch2 = nombreArch.replace(".xls","").trim();
            String nombreArchivo = nombreArch2+".xls";
            File archivo = new File(ruta,nombreArchivo);
            ExcelTableExporter excel = new ExcelTableExporter(tabla, archivo, nombreArchivo);
            excel.export();
            this.setVisible(false);
        }
        else
        {
            this.setVisible(false);
        }
    }//GEN-LAST:event_jFileChooser1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser jFileChooser1;
    // End of variables declaration//GEN-END:variables

}
